package icu.lookyousmileface.nio.basic;

import java.io.*;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

/**
 * @author shenj
 * @title: NioBufferTransorf
 * @projectName NettyPro
 * @date 2020/11/30 20:18
 */
public class NioBufferTransorf {

    private static final String filepath1 = "src/main/resources/filedata/data1.txt";
    private static final String filepath2 = "src/main/resources/filedata/data2.txt";

    public static void main(String[] args) {

        try {

            FileInputStream fileInputStream = new FileInputStream(new File(filepath1));
            FileOutputStream fileOutputStream = new FileOutputStream(new File(filepath2));

            FileChannel fileInputStreamChannel = fileInputStream.getChannel();
            FileChannel fileOutputStreamChannel = fileOutputStream.getChannel();

            //将目标的通道的数据复制到当前通道,Channel自带数据有效长度size获取
            fileOutputStreamChannel.transferFrom(fileInputStreamChannel, 0, fileInputStreamChannel.size());

            fileInputStreamChannel.close();
            fileOutputStreamChannel.close();
            fileInputStream.close();
            fileOutputStream.close();


        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
